package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.CategoryMapper;
import com.itheima.pojo.Category;
import com.itheima.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 放学后海堤日记
 * @Date: 2023/4/13 21:53
 * @Desc:
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Page<Category> listAll(Integer page, Integer pageSize) {
        Page<Category> page1 = new Page<>(page, pageSize);
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort");

        return categoryMapper.selectPage(page1, wrapper);
    }

    @Override
    public int updateCategoryInfo(Category category) {
        int i = categoryMapper.updateById(category);

        return i;
    }

    @Override
    public int deleteCategory(Long ids) {
        int i = categoryMapper.deleteById(ids);

        return i;
    }

    @Override
    public int addCategory(Category category) {
        int i = categoryMapper.insert(category);

        return i;
    }

    @Override
    public String findCategoryById(Long id) {
        Category category = categoryMapper.selectById(id);

        return category.getName();
    }

    public List<Category> listCategoryByType(Integer type) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        if (type != null)
            wrapper.eq("type", type);
        else
            wrapper = null;

        return categoryMapper.selectList(wrapper);
    }
}
